package com.licode.codekit.buildprj;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.licode.codekit.utils.PomFile;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author licode
 */
public class ProjectBuilder {
    private boolean needInit = true;
    private boolean prjExist = false;
    private boolean emptyDir = true;
    private String prjRootDir;
    private String prjRootName;
    private String prjGroupId;
    private String prjArtifactId;
    private Set<String> modules = new HashSet<>();

    public ProjectBuilder() {
    }

    public Set<String> getModules() {
        return modules;
    }

    public void reset() {
        needInit = true;
    }

    public boolean isNeedInit() {
        return needInit;
    }

    public void setModules(Set<String> modules) {
        this.modules = modules;
    }

    public boolean containModule(String mdName) {
        return modules.contains(mdName);
    }

    public String getPrjGroupId() {
        return this.prjGroupId;
    }

    public String getPrjArtifactId() {
        return this.prjArtifactId;
    }

    public boolean isPrjExist() {
        return this.prjExist;
    }

    public void setPrjExist(boolean prjExist) {
        this.prjExist = prjExist;
        this.needInit = false;
    }

    public boolean isEmptyDir() {
        return this.emptyDir;
    }

    public String getPrjRootName() {
        return prjRootName;
    }

    public String getPrjRootDir() {
        return this.prjRootDir;
    }

    public void setPrjRootDir(String prjRootDir) {
        this.prjRootDir = prjRootDir;
    }

    public void createPrj(ProjectContext prjCtx) throws IOException {
        ProjectCreator projectCreator = new ProjectCreator();
        projectCreator.createPrj(prjCtx);
    }

    public boolean createModule(Project rootPrj, ProjectContext prjCtx, String templateFile) throws IOException {
        ProjectCreator projectCreator = new ProjectCreator();
        return projectCreator.createModule(rootPrj, prjCtx, templateFile);
    }

    public String setAndCheckProjectDir(String prjDir) {
        this.modules.clear();
        this.prjGroupId = "";
        this.prjArtifactId = "";
        this.prjRootName = "";
        this.prjExist = false;
        this.emptyDir = false;
        this.needInit = false;

        if (StringUtil.isEmpty(prjDir)) {
            return "无效的工程目录";
        } else {
            int pomCount = 0;
            File pomFile = null;
            File dirFile = new File(prjDir);
            if (dirFile.exists() && !dirFile.isFile()) {
                File[] childrenFiles = dirFile.listFiles();
                if (!Objects.isNull(childrenFiles) && childrenFiles.length != 0) {
                    File[] var6 = childrenFiles;
                    int var7 = childrenFiles.length;

                    for(int var8 = 0; var8 < var7; ++var8) {
                        File childFile = var6[var8];
                        if (childFile.isFile()) {
                            if (childFile.getName().endsWith("pom.xml")) {
                                ++pomCount;
                                pomFile = childFile;
                            }
                        } else if (childFile.isDirectory() && childFile.getName().equals("src")) {
                            return "工程目录不为空";
                        }
                    }

                    if (pomCount != 1) {
                        return "工程目录不为空";
                    } else if (this.checkAndParsePomFile(prjDir, pomFile)) {
                        return this.handleDirAndModalName(prjDir);
                    } else {
                        return "工程目录不为空";
                    }
                } else {
                    this.emptyDir = true;
                    this.handleDirAndModalName(prjDir);
                    return "";
                }
            } else {
                return "工程目录不存在";
            }
        }
    }

    private String handleDirAndModalName(String prjDir) {
        int pos = prjDir.lastIndexOf("/");
        if (-1 == pos) {
            pos = prjDir.lastIndexOf("\\");
        }

        if (-1 == pos) {
            return "非法工程目录";
        } else {
            this.setPrjRootDir(prjDir);
            this.prjRootName = prjDir.substring(pos + 1);
            return StringUtil.isEmpty(prjRootName) ? "非法工程目录" : "";
        }
    }

    private boolean checkAndParsePomFile(String prjDir, File pomFile) {
        PomFile filePom = new PomFile();
        filePom.parse(pomFile.getAbsolutePath());

        try {
            String packaging = filePom.getPackaging();
            if (!"pom".equals(packaging)) {
                return false;
            }

            String groupId = filePom.getGroupId();
            String artifactId = filePom.getArtifactId();
            if (StringUtil.isEmpty(groupId) || StringUtil.isEmpty(artifactId)) {
                return false;
            }

            this.prjGroupId = groupId;
            this.prjArtifactId = artifactId;
            this.prjExist = true;

            this.setModules(filePom.getModules());
        } catch (Exception var18) {
            var18.printStackTrace();
        }

        return true;
    }
}
